package cn.itlight.controller;

import cn.itlight.basic.resultBase;
import cn.itlight.domain.Store;
import cn.itlight.service.IStoreService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.utils.FallbackMethod;
import feign.hystrix.FallbackFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StoreController {

    @Autowired
    private IStoreService storeService;

    @PostMapping("/store/add")
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    public resultBase add(@RequestBody Store store){
        try {
            System.out.println(store);
            storeService.add(store);
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(true);
            resultBase.setMessage("添加成功");
            return resultBase;
        }catch (Exception e){
            e.printStackTrace();
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(false);
            resultBase.setMessage("失败请重试");
            return resultBase;
        }

    }

    @DeleteMapping("/store/remove")
    public resultBase remove(Long id){
        try {
            storeService.remove(id);
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(true);
            resultBase.setMessage("删除成功");
            return resultBase;
        }catch (Exception e){
            e.printStackTrace();
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(false);
            resultBase.setMessage("失败请重试");
            return resultBase;
        }

    }

    @PutMapping("/store/upt")
    public resultBase upt(@RequestBody Store store){
        try {
            storeService.upt(store);
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(true);
            resultBase.setMessage("修改成功");
            return resultBase;
        }catch (Exception e){
            e.printStackTrace();
            resultBase resultBase = new resultBase();
            resultBase.setSuccess(false);
            resultBase.setMessage("失败请重试");
            return resultBase;
        }
    }


    public resultBase fallbackMethod(@RequestBody Store store){
        resultBase resultBase = new resultBase();
        resultBase.setSuccess(false);
        resultBase.setMessage("失败请重试");

        return resultBase;
    }

}
